package com.tjetc.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tjetc.common.Result;
import com.tjetc.domain.Order;
import com.tjetc.service.OrderService;
import com.tjetc.service.impl.OrderServiceImpl;
import com.tjetc.utils.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.sql.Date;
import java.util.List;

@WebServlet("/order/*")
@MultipartConfig
public class OrderServlet extends HttpServlet {
    private OrderService orderService = new OrderServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        String methodName = uri.substring(uri.lastIndexOf("/") + 1);
        try {
            Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this,request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //患者进行预约
    protected void listOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        Integer did = Integer.valueOf(request.getParameter("did"));
        Date date = Date.valueOf(request.getParameter("date"));
        String time = request.getParameter("time");
        Order order = new Order();
        order.setDate(date);
        order.setUid(uid);
        order.setDid(did);
        order.setTime(time);
        boolean b = orderService.addOrder(order);
        if (b){
            new ObjectMapper().writeValue(response.getOutputStream(), Result.success("预约成功"));
        }else{
            new ObjectMapper().writeValue(response.getOutputStream(), Result.fail("当前系统繁忙，请稍后预约"));
        }
    }

    protected void listUserOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        List<Order> orders = orderService.listUserOrder(uid);
        new ObjectMapper().writeValue(response.getOutputStream(),orders);
    }

    //后台管理我的预约信息
    protected void listMyOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer did = Integer.valueOf(request.getParameter("did"));
        Integer status = Integer.valueOf(request.getParameter("status"));
        String curPage = request.getParameter("curPage");
        String ps = request.getParameter("pageSize");
        //接受请求参数
        //空处理:赋值给当前页码
        int currentPage = curPage == null ? 1:Integer.valueOf(curPage);
        //每页显示的条数
        int pageSize = ps == null ? 5 : Integer.parseInt(ps);
        //总记录数
        int count = orderService.getCount(did,status);
        PageInfo<Order> pageInfo = new PageInfo<>(currentPage, count, pageSize);
        List<Order> list = orderService.listMyOrder(pageInfo,did,status);
        pageInfo.setList(list);
        new ObjectMapper().writeValue(response.getOutputStream(),pageInfo);
    }

    //查询某医生的已经被预约的时间
    protected void listMySpareTime(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer did = Integer.valueOf(request.getParameter("did"));
        Date date = Date.valueOf(request.getParameter("date"));
        List<Order> timeList = orderService.listMyOrderTime(did,date);
        new ObjectMapper().writeValue(response.getOutputStream(),timeList);
    }

    //查询有没有未就诊的之前的预约
    protected void listInOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer did = Integer.valueOf(request.getParameter("did"));
        Date date = Date.valueOf(request.getParameter("date"));
        String time = request.getParameter("time");
        boolean b = orderService.listInOrder(did,date,time);
        if (b){
            new ObjectMapper().writeValue(response.getOutputStream(),Result.success("可以就诊"));
        }else{
            new ObjectMapper().writeValue(response.getOutputStream(),Result.fail("请依次就诊"));
        }
    }

}
